package doublelist;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo {
    //TreeMap的使用
    //TreeMap是有序的，默认是自然排序，也可以指定Comparator排序
    public static void main(String[] args) {
//        TreeMap<Student,String> tm = new TreeMap<>((o1,o2) -> o2.getAge() - o1.getAge());
        //在 Student 中继承排序方法
        TreeMap<Student,String> tm = new TreeMap<>();
        //添加
        tm.put(new Student("张三",18),"北京");
        tm.put(new Student("李四",25),"上海");
        tm.put(new Student("王五",20),"广州");
        //遍历
        Set<Map.Entry<Student,String>> set = tm.entrySet();
        for (Map.Entry<Student,String> entry : set) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}
